const db = require('../db/index')


// 获取文章分类列表路由
exports.getArticleCateList = (req, res) => {
    // 判断用户是否登录，已经根据express-jwt模块进行了验证
    // 查询文章列表，返回结果
    const select_sql = `SELECT * FROM ARTICLE_CATE WHERE is_delete=0`
    db.query(select_sql, (err, results) => {
        if(err) return res.cc(err)

        res.send({
            status: 1,
            msg: '查询成功',
            results
        })
    })
}


// 新增文章分类
exports.addArticleCate = (req, res) => {
    // 1. 接收参数
    const article_cate = req.body

    // 2. 校验参数

    // 3. 判断文章分类是否存在
    const select_sql = `SELECT * FROM ARTICLE_CATE WHERE name=?`
    db.query(select_sql, article_cate.name, (err, results) => {
        if(err) return res.cc(err)

        if(results.length >= 1) return res.cc('文章已存在，存储失败')

        const select_sql = `SELECT * FROM ARTICLE_CATE WHERE alias=?`
        db.query(select_sql, article_cate.alias, (err, results) => {
            if(err) return res.cc(err)
            if(results.length >= 1) return res.cc('文章已存在，存储失败')
            
            // 4. 存储新文章返回结果
            const insert_sql = `INSERT INTO ARTICLE_CATE set ?`
            db.query(insert_sql, article_cate, (err, results) => {
                if(err) return res.cc(err)
                if(results.affectedRows != 1) return res.cc('存储失败')

                res.cc('存储成功', status=0)
            })

        })
    })
    
}


// 根据id删除文章分类
exports.deleteArticleCate = (req, res) => {
    const article_id = req.query.id
    // console.log('id:', article_id)

    const select_sql = `SELECT * FROM ARTICLE_CATE where id=?`
    db.query(select_sql, article_id, (err, results) => {
        if(err) return res.cc(err)
        if(results.length == 0) return res.cc('该文章不存在')
        
        // const update_sql = `UPDATE ARTICLE_CATE SET is_delete=? WHERE id=?`  // 这种情况db.query的第二个参数直接用数据，按顺序填写相应的值
        const update_sql = `UPDATE ARTICLE_CATE SET ? WHERE id=?`
        db.query(update_sql, [{is_delete: 1}, article_id], (err, results) => {
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc('删除失败')

            res.cc('删除成功', status=0)
        })
    })
}


// 根据id查询文章分类
exports.searchArticleCate = (req, res) => {
    const article_id = req.query.id

    const select_sql = `SELECT * FROM ARTICLE_CATE WHERE id=?`
    db.query(select_sql, article_id, (err, results) => {
        if(err) return res.cc(err)
        if((results.length != 1) || ((results.length == 1) && (results[0].is_delete == 1))) return res.cc('文章不存在')
        // if((results.length == 1) && (results.is_delete == 1)) return res.cc('查到了')

        res.send({
            status: 0,
            results
        })
    })
}


// 根据id更新文章分类
exports.updateArticleCate = (req, res) => {
    const article_cate = req.body

    const select_sql = `SELECT * FROM ARTICLE_CATE WHERE id=?`
    db.query(select_sql, article_cate.id, (err, results) => {
        if(err) return res.cc(err)
        if((results.length != 1) || ((results.length == 1) && (results[0].is_delete == 1))) return res.cc('该文章不存在')

        const update_sql = `UPDATE ARTICLE_CATE SET ? WHERE id=?`
        db.query(update_sql, [article_cate, article_cate.id], (err, results) => {
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc('更新失败')

            res.cc('更新成功', status=0)
        })
    })
}

